//
//  HospitalViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/20.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "HospitalViewController.h"
#import "HospitalTableViewCell.h"
#import "SubmitTableViewCell.h"
#import "UIButton+Block.h"
@interface HospitalViewController ()

@property (nonatomic,strong)UIButton * choiceButtonType;

@end

@implementation HospitalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBtn:nil];
    self.title=@"录入点";

    [self initWithTableViewNib:@"HospitalTableViewCell" didSelectCellBlock:^(NSIndexPath *indexPath) {
//        [weakSelf didselectRow:indexPath];
        
        
    }];
    FRAME( self.tableview
          , 0, 44+64, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self registerNib:@"SubmitTableViewCell"];
    
    [self setRequest];
    
    
    // Do any additional setup after loading the view from its nib.
}

-(void)setRequest{
  
    
    [self requestApiWithUrl:@"/hospital/getHospitalList" dictionary:nil isCache:YES  success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"hospitalList"]) {
            
            [self.dataArray addObject:dic];
            
        }
        
        [self.tableview reloadData];
        
        NSLog(@"%@",responseObject);
        
    } faild:^(NSError *error) {
        
    }];
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
      if (indexPath.row==self.dataArray.count) {
        
        SubmitTableViewCell  *cell =[tableView dequeueReusableCellWithIdentifier:@"SubmitTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
          [cell.submitBtn setTitle:@"确定" forState:UIControlStateNormal];
          
          [cell.submitBtn addActionHandler:^(NSInteger tag) {
//              NSLog(@"---%@",self.dataArray[tag-200][@"name"]);
              
              if (_SelectIndexName) {
                  if (self.choiceButtonType.selected) {
                      
                 
                  _SelectIndexName(@[self.dataArray[self.choiceButtonType.tag-200][@"name"],self.dataArray[self.choiceButtonType.tag-200][@"id"]]);
                   [self.navigationController popViewControllerAnimated:YES];
                   }
                  else
                  {
                      [self.view makeToast:@"请选择一个医院"];
                     

                  }
              }
              
 
          }];
        return cell;

    }

    else
    {
        HospitalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HospitalTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.title.text=self.dataArray[indexPath.row][@"name"];
        
        cell.selectView.tag=indexPath.row+200;
        self.choiceButtonType=cell.selectView;

        [cell.selectView setImage:[UIImage imageNamed:@"dui"] forState:UIControlStateSelected];
        [cell.selectView setImage:nil forState:UIControlStateNormal];
        if (indexPath.row==self.dataArray.count-1) {
            cell.diImg.hidden=YES;
            
        }
        
        return cell;
        
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count+1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==self.dataArray.count) {

    return 70;
    }
    else
        return 44;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UIButton *btn = [self.view viewWithTag:indexPath.row +200];
    self.choiceButtonType.selected=NO;

    btn.selected=YES;

    self.choiceButtonType=btn;
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
